home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / gawk-3.000 / gawk-3 / gawk-3.0.0 / pc / awklib / igawk.awk next >
Encoding:
AWK Script  |  1995-12-15  |  1.3 KB  |  52 lines

  1. # igawk.awk
  2. # process @include directives
  3.  
  4. function pathto(file,    i, t, junk)
  5. {
  6.     if (index(file, "/") != 0)
  7.         return file
  8.  
  9.     for (i = 1; i <= ndirs; i++) {
  10.         t = (pathlist[i] "/" file)
  11.         if ((getline junk < t) > 0) {
  12.             # found it
  13.             close(t)
  14.             return t
  15.         }
  16.     }
  17.     return ""
  18. }
  19. BEGIN {
  20.     path = ENVIRON["AWKPATH"]
  21.     ndirs = split(path, pathlist, ";")
  22.     for (i = 1; i <= ndirs; i++) {
  23.         if (pathlist[i] == "")
  24.             pathlist[i] = "."
  25.     }
  26.     stackptr = 0
  27.     input[stackptr] = ARGV[1] # ARGV[1] is first file
  28.  
  29.     for (; stackptr >= 0; stackptr--) {
  30.         while ((getline < input[stackptr]) > 0) {
  31.             if (tolower($1) != "@include") {
  32.                 print
  33.                 continue
  34.             }
  35.             fpath = pathto($2)
  36.             if (fpath == "") {
  37.                 printf("igawk:%s:%d: cannot find %s\n", \
  38.                     input[stackptr], FNR, $2) > "/dev/stderr"
  39.                 continue
  40.             }
  41.             if (! (fpath in processed)) {
  42.                 processed[fpath] = input[stackptr]
  43.                 input[++stackptr] = fpath
  44.             } else
  45.                 print $2, "included in", input[stackptr], \
  46.                     "already included in", \
  47.                     processed[fpath] > "/dev/stderr"
  48.         }
  49.         close(input[stackptr])
  50.     }
  51. }
  52.